package com.nsgf.library.ui.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.google.gson.JsonObject;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.data.bean.AddressBean;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.utils.UnitTo;
import com.nsgf.library.widget.CheckEditForButton;
import com.nsgf.library.widget.EditTextChangeListener;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ActivityAddAddressListBinding;

public class AddAddressListActivity extends BaseActivity implements EditTextChangeListener {

    private CheckEditForButton checkEditForButton;


    private AddressBean addressBean;

    private AccountRequester mAccountRequester;

    public JsonObject getRequestParams() {
        JsonObject params = new JsonObject();
        params.addProperty("name", mStates.nameAddress.get());
        params.addProperty("mobile", mStates.phoneAddress.get());
        params.addProperty("detailAddress", mStates.detailAddress.get());
        params.addProperty("defaultStatus", mStates.isDefault.get());

        return params;
    }

    @Override
    public void allHasContent(boolean isHasContent) {

    }

    private AddAddressStates mStates;

    @Override
    protected void initViewModel() {
        mStates = new AddAddressStates();
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_add_address_list, BR.vm, mStates)
                .addBindingParam(BR.click, new ClickProxy());
    }

    private ActivityAddAddressListBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mBinding = (ActivityAddAddressListBinding) getBinding();
        addressBean = getIntent().getParcelableExtra("data");
        if (null != addressBean) {
            mStates.phoneAddress.set(addressBean.getMobile());
            mStates.nameAddress.set(addressBean.getName());
            mStates.detailAddress.set(addressBean.getDetailAddress());
            mStates.isDefault.set(addressBean.isDefaultStatus());
            mStates.titleBar.set("修改收货地址");
        }else {
            mStates.titleBar.set("添加收货地址");}
        mAccountRequester.getSrcSuccess().observe(this, new Observer<ResponseStatus<String>>() {
            @Override
            public void onChanged(ResponseStatus<String> stringResponseStatus) {
                if (stringResponseStatus.getResponseCode() == 0) {
                    Toast.makeText(AddAddressListActivity.this, stringResponseStatus.getMsg(), Toast.LENGTH_LONG).show();
                    finish();
                }
            }
        });
    }


    public static class AddAddressStates extends StateHolder {

        public final State<String> nameAddress = new State<>("");

        public final State<String> phoneAddress = new State<>("");
        public final State<String> detailAddress = new State<>("");
        public final State<Boolean> isDefault = new State<>(false);

    }

    public class ClickProxy {

        public void addressSubmit() {

            if (TextUtils.isEmpty(mStates.name.get())
                    || TextUtils.isEmpty(mStates.phoneAddress.get())
                    || TextUtils.isEmpty(mStates.detailAddress.get())) {

                Toast.makeText(AddAddressListActivity.this, "收货地址填写不完整", Toast.LENGTH_SHORT).show();
                return;
            }

            getController().displayLoading();

            if (null != addressBean) {

                JsonObject params = getRequestParams();
                params.addProperty("id", addressBean.getId());
                params.addProperty("areaId", addressBean.getAreaId());
                mAccountRequester.modifyAddress(params);
            } else {
                mAccountRequester.createAddress(getRequestParams());
            }
        }
    }
}
